﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

using zevenseas.LifeCycleManagement.Utilities;
using zevenseas.LifeCycleManagement.TimerJobs;

namespace zevenseas.LifeCycleManagement.ApplicationPages
{
    public class CaptureDeletionSettings : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        private ErrorHandling errorHandling;

        protected Label output;
                
        protected WebApplicationSelector webApplicationSelector;

        protected InputFormCheckBox cbxDateTime;
        protected InputFormCheckBox cbxFolderSitecollection;
        protected InputFormCheckBox cbxFolderWeb;
        protected InputFormCheckBox cbxLogInSharePoint;
        protected InputFormTextBox txtBackuplocation;
        protected Button btnOk;
        protected LinkButton btnInstall;
        protected LinkButton btnCheck;
        
        protected Literal Result;

        public CaptureDeletionSettings()
        {
            errorHandling = new ErrorHandling();
            errorHandling.productName = "CaptureDeletionSettings";
            errorHandling.exeName = "zevenseas.LifeCycleManagement.ApplicationPages";
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                try
                {
                    SPWebApplication webApplication = webApplicationSelector.CurrentItem;
                    SPWeb web = SPContext.Current.Web;

                    if (webApplication != null)
                    {
                        SiteDeletionCaptureSettings settings = webApplication.GetChild<SiteDeletionCaptureSettings>("SiteDeletionCaptureSettings");
                        if (settings != null)
                        {
                            txtBackuplocation.Text = settings.backupLocation;
                            cbxDateTime.Checked = settings.dateTime;
                            cbxFolderSitecollection.Checked = settings.folderSitecollection;
                            cbxFolderWeb.Checked = settings.folderWeb;
                            cbxLogInSharePoint.Checked = settings.logInSharePoint;
                        }

                        if (webApplication.JobDefinitions.GetValue<InstallEventHandlerTimerjob>("LCMInstallEventHandlerTimerjob") != null)
                        {
                            InstallEventHandlerTimerjob installEventHandlerTimerjob = webApplication.JobDefinitions.GetValue<InstallEventHandlerTimerjob>("LCMInstallEventHandlerTimerjob");                                
                            Result.Text = "Timer job is running and processing all the webs";
                            btnInstall.Enabled = false;
                            btnCheck.Enabled = false;
                        }

                        EventHandlerStatus status = webApplication.GetChild<EventHandlerStatus>("EventHandlerStatus");
                        if (status != null)
                        {
                            Result.Text += "<BR>"+ status.WebsProcessed.ToString() + " webs processed";
                            Result.Text += "<BR>Installed the eventhandler " +status.FeatureInstalled.ToString()+ " times";
                            Result.Text += "<BR>Eventhandler already installed on " + status.FeatureAlreadyInstalled.ToString() + " webs";
                        }
                    }
                }
                catch (Exception configGetError)
                {
                    errorHandling.LogMessage(configGetError.Message.ToString(), configGetError);
                }
            }

            base.OnLoadComplete(e);
        }       
        
        protected void OnClickOK(Object Sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;

            try
            {
                SPWebApplication webApplication = webApplicationSelector.CurrentItem;

                try
                {                   
                    SiteDeletionCaptureSettings settings = webApplication.GetChild<SiteDeletionCaptureSettings>("SiteDeletionCaptureSettings");
                    
                    // If no settings previously created, create them now.    
                    if (settings == null)
                    {
                        SPPersistedObject parent = webApplication;
                        settings = new SiteDeletionCaptureSettings("SiteDeletionCaptureSettings", parent, Guid.NewGuid());                        
                        settings.Update();
                    }

                    settings.dateTime = cbxDateTime.Checked;
                    settings.folderSitecollection = cbxFolderSitecollection.Checked;
                    settings.folderWeb = cbxFolderWeb.Checked;
                    settings.backupLocation = txtBackuplocation.Text;
                    settings.logInSharePoint = cbxLogInSharePoint.Checked;

                    settings.Update();
                }

                catch (Exception error)
                {
                    errorHandling.LogMessage("Storing the configuration failed: " + error.Message.ToString());
                }

            }
            catch (Exception configsetError)
            {
                errorHandling.LogMessage(configsetError.Message.ToString());
            }

            this.Page.Response.Redirect("/_admin/applications.aspx");
        }       
        
        protected void OnClickCancel(Object Sender, EventArgs e)
        {
            this.Page.Response.Redirect("/_admin/applications.aspx");
        }

        protected void btn_Install(Object Sender, EventArgs e)
        {
            SPWebApplication local = webApplicationSelector.CurrentItem;
            if (local.JobDefinitions.GetValue<InstallEventHandlerTimerjob>("LCMInstallEventHandlerTimerjob") == null)
            {
                InstallEventHandlerTimerjob installEventHandlerTimerjob = new InstallEventHandlerTimerjob("LCMInstallEventHandlerTimerjob", local, null, SPJobLockType.Job);
                SPOneTimeSchedule schedule = new SPOneTimeSchedule(DateTime.Now);
                installEventHandlerTimerjob.Schedule = schedule;
                installEventHandlerTimerjob.Name = "LCMInstallEventHandlerTimerjob";
                installEventHandlerTimerjob.Title = "LCMInstallEventHandlerTimerjob";
                installEventHandlerTimerjob.Installing = true;
                installEventHandlerTimerjob.Update();
            }

            btnInstall.Enabled = false;
            btnCheck.Enabled = false;

            this.Page.Response.Redirect("/_admin/LCM/capturedeletion.aspx");
        }

        protected void btn_Check(Object Sender, EventArgs e)
        {
            SPWebApplication local = webApplicationSelector.CurrentItem;
            if (local.JobDefinitions.GetValue<InstallEventHandlerTimerjob>("LCMInstallEventHandlerTimerjob") == null)
            {
                InstallEventHandlerTimerjob installEventHandlerTimerjob = new InstallEventHandlerTimerjob("LCMInstallEventHandlerTimerjob", local, null, SPJobLockType.Job);
                SPOneTimeSchedule schedule = new SPOneTimeSchedule(DateTime.Now);
                installEventHandlerTimerjob.Schedule = schedule;
                installEventHandlerTimerjob.Name = "LCMInstallEventHandlerTimerjob";
                installEventHandlerTimerjob.Title = "LCMInstallEventHandlerTimerjob";
                installEventHandlerTimerjob.Installing = false;
                installEventHandlerTimerjob.Update();
            }

            btnInstall.Enabled = false;
            btnCheck.Enabled = false;
        }
    }
}
